﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Teleplex.Data;
using Teleplex.Data.DataAdapters.Adapters;
using Teleplex.Data.DataAdapters.Interfaces;
using Teleplex.DataModels;

namespace Teleplex.Web.Controllers
{
    public class TVCharacterController : Controller
    {

        public ITVShowAdapter _adapter;

        public TVCharacterController()
        {
            _adapter = new TVShowAdapter();
        }
        public TVCharacterController(TVShowAdapter adapter)
        {
            _adapter = adapter;
        }
        // GET: TVCharacter
        // Main index view for TV Characters
        public ActionResult Index()
        {
            // TODO: add paging
            List<TVCharacter> characters = _adapter.GetAllCharacters();
            return View(characters);
        }

        //Character Details view
        public ActionResult Detail(int id = 0)
        {
            if(id <= 0)
            {
                return RedirectToAction("Index");
            }

            TVCharacter character = _adapter.GetCharacter(id);

            return View(character);
        }

        //Character Edit view
        public ActionResult EditCharacter(int id)
        {
            TVCharacter character = _adapter.GetCharacter(id);

            return View(character);
        }
        [HttpPost]
        //public ActionResult EditCharacter(int charId, string name, string bio, string actor, string imageURL)
        public ActionResult EditCharacter(TVCharacter character)
        {
            character = _adapter.UpdateCharacter(character);
            
            return RedirectToAction("Detail", new { id = character.Id });
        }

        //Character Add view (only accessible from TV show details)
        public ActionResult AddCharacter(int id)
        {
            TVShow tvShow = _adapter.GetShow(id);

            return View(tvShow);
        }
        [HttpPost]
        public ActionResult AddCharacter(int showId, string name, string bio, string actor, string imageURL)
        {
            TVCharacter character = new TVCharacter(showId, name, bio, actor, imageURL);
            character = _adapter.CreateCharacter(character);
            return RedirectToAction("Detail", "TVShow", new { id = showId });
        }

        //Character Delete action
        [HttpPost]
        public ActionResult DeleteCharacter(int id)
        {
            TVCharacter character = _adapter.DeleteCharacter(id);

            return RedirectToAction("Detail","TVShow",new {id = character.TVShowId});

        }
    }
}